﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DrawTools;
using System.Drawing;
using System.Drawing.Drawing2D;
using KHMI.Attributes;

namespace DrawTools
{
    public class DrawWatch2 : DrawRectangle
    {
        const double PEI = 3.1415926;

        private int mV;
        private int mMaxMain;

        [ExtendCategoryAttribute("仪表设置", VectorName = "当前刻度1")]
        public int V//当前刻度１
        {
            get
            {
                return mV;
            }
            set
            {
                if (mV == value) return;
                mV = value;
            }
        }

        [ExtendCategoryAttribute("仪表设置", VectorName = "最大刻度")]
        public int MaxMain//最大刻度
        {
            get
            {
                return mMaxMain;
            }
            set
            {
                if (mMaxMain == value) return;
                mMaxMain = value;
            }
        }

        public DrawWatch2() : this(0, 0, 1, 1)
		{
		}

        public DrawWatch2(int x, int y, int width, int height)
            : base()
        {
            Rectangle = new Rectangle(x, y, width, height);
            Initialize();
        }

        /// <summary>
        /// Clone this instance
        /// </summary>
        public override DrawObject Clone(DrawObject drawObject)
        {
            base.Clone(drawObject);
            return this;
        }

        public override void SaveToStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
        {
            //info.AddValue("cube" + orderNumber.ToString(), Cube);

            base.SaveToStream(info, orderNumber);
        }

        public override void LoadFromStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
        {
            //Cube = info.GetInt32("cube" + orderNumber.ToString());

            base.LoadFromStream(info, orderNumber);
        }

        /// <summary>
        /// Draw Watch
        /// </summary>
        /// <param name="g"></param>
        public override void Draw(Graphics graphics)
        {
            try
            {
                Rectangle clientrect = DrawRectangle.GetNormalizedRectangle(Rectangle);

                //设置路码表的高和宽
                float WIDTH = clientrect.X + clientrect.Width;
                float HEIGHT = clientrect.Y + clientrect.Height;

                FontFamily fontFamily = new FontFamily("Times New Roman");
                Font font = new Font(fontFamily, 14, FontStyle.Bold, GraphicsUnit.Pixel);

                //将绘图平面的坐标原点移到窗口中心
                graphics.TranslateTransform(WIDTH / 2, HEIGHT / 2);
                //绘制仪表盘
                graphics.FillEllipse(Brushes.Black,
                    HEIGHT / -2, HEIGHT / -2, HEIGHT, HEIGHT);
                //输出文本
                graphics.DrawString("公里/小时", font, Brushes.Green,
                    new PointF(-26, HEIGHT / -4 - font.Height));

                //绘制刻度标记
                graphics.RotateTransform(225);
                for (int x = 0; x < 55; x++)
                {
                    graphics.FillRectangle(Brushes.Green,
                        -2, (HEIGHT / 2 - 2) * -1, 3, 15);
                    graphics.RotateTransform(5);
                }

                //重置绘图平面的坐标变换
                graphics.ResetTransform();

                graphics.TranslateTransform(WIDTH / 2, HEIGHT / 2);
                graphics.RotateTransform(225);
                int sp = 0;
                string tmp = string.Empty;
                //绘制刻度值(整数类)
                for (int x = 0; x < 7; x++)
                {
                    tmp = string.Format("{0}", sp);

                    //绘制红色刻度
                    graphics.FillRectangle(Brushes.Red, -3, (HEIGHT / 2 - 2) * -1, 6, 25);
                    //绘制数值
                    graphics.DrawString(tmp, font, Brushes.Green, new PointF(tmp.Length * (-6),
                        (HEIGHT / -2) + 25));
                    //旋转45度
                    graphics.RotateTransform(45);
                    sp += 20;
                }

                //重置绘图平面的坐标变换
                graphics.ResetTransform();
                graphics.TranslateTransform(WIDTH / 2, HEIGHT / 2);

                //绘制指针在30公里/秒的情形
                float angle = 30 * 2.25f + 225;
                graphics.RotateTransform((float)angle);

                Pen pen = new Pen(Color.Blue, 14);
                //设置线帽
                pen.EndCap = LineCap.ArrowAnchor;
                pen.StartCap = LineCap.RoundAnchor;
                //画指针
                graphics.DrawLine(pen, new PointF(0, 0), new PointF(0, (-1) * (HEIGHT / 2.75F)));

                graphics.ResetTransform();
                graphics.TranslateTransform(WIDTH / 2, HEIGHT / 2);
                //绘制中心点
                graphics.FillEllipse(Brushes.Black, -6, -9, 14, 14);
                graphics.FillEllipse(Brushes.Red, -7, -7, 14, 14);

                //绘制速度极限标记
                pen.Width = 4;
                pen.Color = Color.Black;
                pen.EndCap = LineCap.Round;
                pen.StartCap = LineCap.Flat;
                graphics.DrawLine(pen, new PointF(HEIGHT / 15.75F, HEIGHT / 3.95F),
                    new PointF(HEIGHT / 10.75F, HEIGHT / 5.2F));
                pen.Color = Color.Red;
                graphics.DrawLine(pen, new PointF(HEIGHT / 15.75F, HEIGHT / 3.95F),
                    new PointF(HEIGHT / 15.75F, HEIGHT / 4.6F));
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

        private Point Trans(int r, double rad, int ox, int oy)
        {
            Point pt = new Point(0, 0);
            int xx = (int)(r * Math.Cos(rad));
            int yy = (int)(r * Math.Sin(rad));
            pt.Y = -yy;
            pt.X = xx;
            pt.X += ox;
            pt.Y += oy;
            return pt;

        }
    }
}
